package com.bjqh.edu.UI.Activity.StudentPersonal;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bjqh.edu.Base.AppConstants;
import com.bjqh.edu.Bean.History;
import com.bjqh.edu.R;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;

import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

public class MyGradeActivity extends AppCompatActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_qx)
    TextView tvQx;
    @BindView(R.id.rl_top_tilte)
    RelativeLayout rlTopTilte;
    @BindView(R.id.et_cj)
    EditText etCj;
    @BindView(R.id.et_subject)
    TextView etSubject;
    @BindView(R.id.btn_submit)
    Button btnSubmit;
    @BindView(R.id.iv_course)
    LinearLayout ivCourse;
    String[] course ;
    String[] grade ;
    String courseId;
    String gradeId;
    Dialog alertDialog;
    Dialog alertDialog2;
    History history;
    public int selectedCourseIndex = 0;
    public int selectedGradeIndex = 0;
    int mYear, mMonth, mDay;
    final int DATE_DIALOG = 1;
    List<History.CourseBean> curseBean;
    List<History.GradeBean> gradeBeen;
    @BindView(R.id.et_time)
    TextView etTime;
    @BindView(R.id.ll_time)
    LinearLayout llTime;
    @BindView(R.id.et_grade)
    TextView etGrade;
    @BindView(R.id.iv_grade)
    LinearLayout ivGrade;
    private ProgressDialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_my_grade);
        ButterKnife.bind(this);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tvQx.setVisibility(View.VISIBLE);
        tvTitle.setText("添加成绩");
        tvQx.setText("历史成绩");
        tvQx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MyGradeActivity.this, HistoricalGradeActivity.class));
            }
        });

        dialog = new ProgressDialog(this);
        dialog.setMessage("火速加载中…");
        dialog.show();
        OkGo.post(AppConstants.BASE_URL + "/admin/menuicon/gradeAndCourse.shtml")
                .tag(this)
                .cacheKey("cacheKey")
                .cacheMode(CacheMode.DEFAULT)
                .params("flag", "1")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        AppConstants.removeDialog(dialog);
                        Log.e("学科", s);
                        Gson gson = new Gson();
                        History history = gson.fromJson(s, History.class);
                        gradeBeen = history.getGrade();
                        curseBean = history.getCourse();
                        course=new String[curseBean.size()];
                        grade=new String[gradeBeen.size()];
                        for (int i = 0; i < curseBean.size(); i++) {
                            course[i] = curseBean.get(i).getName();
                        }
                        for (int i = 0; i < gradeBeen.size(); i++) {
                            grade[i] = gradeBeen.get(i).getName();
                        }

                        alertDialog = new AlertDialog.Builder(MyGradeActivity.this).setTitle("选择年级").setSingleChoiceItems(grade, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                selectedGradeIndex = which;
                                //sub = gradeBean.get(which).getCname();
                                gradeId = gradeBeen.get(which).getId();
                            }
                        })
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {

                                        etGrade.setText(grade[selectedGradeIndex]);
                                        dialog.dismiss();
                                    }

                                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                }).create();
                        alertDialog2 = new AlertDialog.Builder(MyGradeActivity.this).setTitle("选择学科").setSingleChoiceItems(course, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                selectedCourseIndex = which;
                                //sub = gradeBean.get(which).getCname();
                                courseId = curseBean.get(which).getId();
                            }
                        })
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {

                                        etSubject.setText(course[selectedCourseIndex]);
                                        dialog.dismiss();
                                    }

                                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                }).create();

                    }
                });
        ivGrade.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                alertDialog.show();
            }
        });
        ivCourse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                alertDialog2.show();

            }
        });
        llTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog(DATE_DIALOG);

            }
        });
        final Calendar ca = Calendar.getInstance();
        mYear = ca.get(Calendar.YEAR);
        mMonth = ca.get(Calendar.MONTH);
        mDay = ca.get(Calendar.DAY_OF_MONTH);

        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("时间", etTime.getText().toString());
                if (TextUtils.isEmpty(etTime.getText())) {

                    Toast.makeText(MyGradeActivity.this, "小主，您的时间还未选择哦~~", Toast.LENGTH_SHORT).show();
                    etTime.requestFocus();
                    return;

                }
                if (TextUtils.isEmpty(etCj.getText())) {
                    Toast.makeText(MyGradeActivity.this, "小主，您的分数还未填写哦~~", Toast.LENGTH_SHORT).show();
                    etCj.requestFocus();
                    return;

                }
                if (TextUtils.isEmpty(etGrade.getText())) {
                    Toast.makeText(MyGradeActivity.this, "小主，您的年级还未选择哦~~", Toast.LENGTH_SHORT).show();
                    etGrade.requestFocus();
                    return;

                }
                if (TextUtils.isEmpty(etSubject.getText())) {
                    Toast.makeText(MyGradeActivity.this, "小主，您的学科还未选择哦~~", Toast.LENGTH_SHORT).show();
                    etSubject.requestFocus();
                    return;

                }


                OkGo.post(AppConstants.BASE_URL + "/grade/addOrUpdate.shtml")
                        .tag(this)
                        .cacheKey("cacheKey")
                        .cacheMode(CacheMode.DEFAULT)
                        .params("scord", etCj.getText().toString())
                        .params("cid", courseId)
                        .params("time", etTime.getText().toString())
                        .params("gradeid", gradeId)
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(String s, Call call, Response response) {

                                Toast.makeText(MyGradeActivity.this, "报告小主，您的要求已完成。", Toast.LENGTH_SHORT).show();
                                Log.e("成绩录入", s);
                                startActivity(new Intent(MyGradeActivity.this,HistoricalGradeActivity.class));
                                finish();

                            }

                            @Override
                            public void onError(Call call, Response response, Exception e) {
                                super.onError(call, response, e);
                                Toast.makeText(MyGradeActivity.this, "报告小主，服务器被外星人偷走啦~~", Toast.LENGTH_SHORT).show();
                            }
                        });
            }
        });


    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG:
                return new DatePickerDialog(this, mdateListener, mYear, mMonth, mDay);
        }
        return null;
    }

    /**
     * 设置日期 利用StringBuffer追加
     */
    public void display() {
        etTime.setText(new StringBuffer().append(mYear).append("-").append(mMonth + 1).append("-").append(mDay));
    }

    private DatePickerDialog.OnDateSetListener mdateListener = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            display();
        }
    };
}
